//
//  B_RealNameViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/17.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_RealNameViewController.h"
#import "B_SubmitFooterView.h"
#import "B_RealNameHeaderView.h"
#import "B_RealNameHeaderTwoView.h"
#import "B_RealNameTVC.h"
#import "B_RealNameFooterView.h"
#import "B_AddBankModel.h"

@interface B_RealNameViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) BaseTableView * mainView;

@property (nonatomic, strong) B_SubmitFooterView * submitView;
@end

@implementation B_RealNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    B_AddBankModel * model0 = [B_AddBankModel initB_AddBankModelWithTitle:@"" placeholder:@"请填写真实姓名" mapword:@"真实姓名" alert:@"请填写真实姓名"];
    B_AddBankModel * model1 = [B_AddBankModel initB_AddBankModelWithTitle:@"" placeholder:@"请填写身份证号" mapword:@"身份证号" alert:@"请填写身份证号"];
    
    self.dataArray = [NSMutableArray arrayWithObjects:model0, model1, nil];
    
    self.view = self.mainView;
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView addSubview:self.submitView];
        [self.submitView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(49));
        }];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (49 + 24) : 49;
        [_mainView setTableViewFrameWithTop:-getNavHeight() left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_RealNameTVC" bundle:nil] forCellReuseIdentifier:@"B_RealNameTVC"];
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 2;
        };
        
        _mainView.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            if (section == 0) {
            return 415;
            } else {
                return 70;
            }
        };
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_RealNameHeaderView * view = [B_RealNameHeaderView initB_RealNameHeaderView];
                
                return view;
            } else {
                B_RealNameHeaderTwoView * view = [B_RealNameHeaderTwoView initB_RealNameHeaderTwoView];
                return view;
            }
        };
        
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 0;
            } else {
                return weSelf.dataArray.count;
            }
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 54;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_AddBankModel * model = weSelf.dataArray[indexPath.row];
            B_RealNameTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_RealNameTVC" forIndexPath:indexPath];
            cell.B_realTextfield.placeholder = model.placeholder_name;
            cell.B_realTextfield.text = model.value_name;
            cell.B_realTextfield.delegate = weSelf;
            cell.B_realTextfield.tag = indexPath.row;
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            
        };
        
        _mainView.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 0.1f;
            } else {
                return 80;
            }
        };
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return nil;
            } else {
                B_RealNameFooterView * footer = [B_RealNameFooterView initB_RealNameFooterView];
                return footer;
            }
        };
        
    }
    return _mainView;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    B_AddBankModel * model = self.dataArray[textField.tag];
    model.value_name = textField.text;
    BOOL canSubmit = YES;
    for (B_AddBankModel * model1 in self.dataArray) {
        if (ISNULL(model1.value_name)) {
            canSubmit = NO;
        }
    }
    if (canSubmit) {
        self.submitView.B_SubmitButton.backgroundColor = [UIColor color_pink];
    }
}

- (B_SubmitFooterView *)submitView {
    if (!_submitView) {
        WeakSelf(weSelf, self);
        _submitView = [B_SubmitFooterView initB_SubmitFooterView];
        [_submitView.B_SubmitButton setTitle:@"提交" forState:UIControlStateNormal];
        _submitView.submitAction = ^{
            //TODO:执行提交操作
            NSString * userName, * idCardNumber;
            
            for (B_AddBankModel * model1 in weSelf.dataArray) {
                if (ISNULL(model1.value_name)) {
                    [KeyWindow makeToast:model1.alert_message];
                    return ;
                }
                if ([model1.map_word isEqualToString:@"身份证号"]) {
                    if ([NSString accurateVerifyIDCardNumber:model1.value_name]) {
                        idCardNumber = model1.value_name;
                    } else {
                        [KeyWindow makeToast:@"请输入正确的身份证号"];
                        return ;
                    }
                }
                if ([model1.map_word isEqualToString:@"真实姓名"]) {
                    userName = model1.value_name;
                }
            }
            
            NSString * stringPath = [NSString stringWithFormat:@"/yearnfor/inter/userAuthentication/userAuthentication/%@/%@/%@", getUserId(), userName, idCardNumber];
            [NetworkingManager postNetworkRequestWithUrlString:stringPath parameters:nil isCache:NO succeed:^(id data) {
                if ([data[@"code"] isEqualToString:@"20005"]) {
                    [KeyWindow makeToast:@"信息提交成功, 请耐心等待审核!"];
                    [weSelf.navigationController popViewControllerAnimated:YES];
                } else {
                    [KeyWindow makeToast:data[@"message"]];
                }
            } fail:^(NSError *error) {
                [KeyWindow makeToast:@"服务器异常"];
            }];
        };
    }
    return _submitView;
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavigationBarBackgroundWithImage:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
    [self setNavigationBarShadowImge:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
